package org.dromara.system.listener;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import lombok.extern.log4j.Log4j2;
import org.dromara.common.core.exception.BusinessException;
import org.dromara.system.config.redis.RedisDelayMQListener;
import org.dromara.system.config.redis.RedisDelayMQMessage;
import org.dromara.system.config.redis.RedisMQListener;
import org.dromara.system.config.redis.RedisMQMessage;
import org.dromara.system.domain.SysUserMachinePolicy;
import org.dromara.system.enums.NotificationEnum;
import org.dromara.system.enums.RedisKeyEnum;
import org.dromara.system.enums.RedisMQConstant;
import org.dromara.system.factory.PlatformTypeFactory;
import org.dromara.system.service.ISysUserMachinePolicyService;
import org.dromara.system.service.impl.trading.AbstractTradingService;
import org.dromara.system.util.JsonUtil;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author lilu
 */
@Log4j2
@Component
public class RedisMQListenerContainer {

    @Autowired
    private RedissonClient redisson;

    @Autowired
    private PlatformTypeFactory platformTypeFactory;

    @Autowired
    private ISysUserMachinePolicyService sysUserMachinePolicyService;

    /* *
     * 处理交易
     * */
    @RedisDelayMQListener(RedisMQConstant.TRADING_QUEUE)
    public void dealKYTMessage(RedisDelayMQMessage<String> message) {
        RLock lock = redisson.getLock(RedisKeyEnum.LOCK_REDIS.getCode().concat(message.getOrderNo()));
        try {
            lock.lock();
            NotificationEnum notificationEnum = message.getNotificationEnum();
            log.info("消息接收 类型:{} data:{}", notificationEnum.getCode(), JSONUtil.toJsonStr(message.getPayload()));
            Object object = JsonUtil.toObject(Convert.toStr(message.getPayload()), notificationEnum.getClazz());
            AbstractTradingService abstractTradingService = platformTypeFactory.get(notificationEnum);
            BusinessException.isNull(abstractTradingService, "渠道不存在");
            abstractTradingService.tradingWaterConvert(object);
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }

    }

    /* *
    * 达标返现
    * */
    @RedisDelayMQListener(RedisMQConstant.COMPLETE_CASH_BACK_QUEUE)
    public void completeCashBack(RedisDelayMQMessage<String> message) {
        String payload = message.getPayload();
        sysUserMachinePolicyService.dealCompleteCashBack(payload);
    }

    /* *
     * 阶段返现
     * */
    @RedisDelayMQListener(RedisMQConstant.STAGE_CASH_BACK_QUEUE)
    public void stageCashBack(RedisDelayMQMessage<String> message) {
        String payload = message.getPayload();
        sysUserMachinePolicyService.dealStageCashBack(payload);
    }







    @RedisMQListener(RedisMQConstant.TEST_QUEUE)
    public void dealRedisMessage(RedisMQMessage message) {
        log.info("dealRedisMessage0收到queue-1队列消息: {}", message);
    }


}
